<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
include_once(APPPATH."libraries/AdminController.php");
class Tawar_asuransi extends AdminController {  
	
	var $status_form = "'ENTRY','UPLOAD'";
	var $dis = "";
	var $is_detail = FALSE;
	
	function __construct()   
	{
		parent::__construct();    
		$this->_set_action();
		$this->_set_action(array("detail"),"ITEM");
		$this->cat_search = array(
			'all'						=> 'Pilih Kategory',
			'form_principal_name'		=> 'Nama Principal',
			'form_obligee_name'			=> 'Nama Obligee',
			'form_bank_garansi_type'	=> 'Tipe Bank Garansi'
			/*'form_obligee_address'		=> 'Alamat Obligee',			
			'form_principal_address'	=> 'Alamat Principal',
			'form_document_number'		=> 'Nomor Dokumen',
			'form_document_desc'		=> 'Penjelasan Dokumen',
			'marketing_name'			=> 'Nama Marketing',
			'cur_name'					=> 'Nama Mata Uang'*/
		);
		$this->_set_title('Transaksi > Data Penawaran Formulir Kepada Asuransi');
		$this->_set_cat_search(array("all"	=> $this->cat_search));	
		$this->DATA->table="tm_formulir";
		$this->load->model("mdl_formulir","A");
		$this->folder_view = "transaksi/";
		$this->prefix_view = strtolower($this->_getClass());
		
		//$this->output->enable_profiler(true);

	}

	function index(){
		
		$this->jCfg['isearch']['status'] = "all";
		$this->_releaseSession();
		
		$this->_set_desc_title('Daftar Penawaran Formulir Kepada Asuransi');	
		
		$mst=isset($this->jCfg['isearch']['status'])?$this->jCfg['isearch']['status']:"all";
		$stat = ($mst=="all")?$this->status_form:"'".$mst."'";
		
		$this->per_page = $this->jCfg['isearch']['per_page'];
		$this->data_table = $this->A->data( array(
				"limit" 	=> $this->per_page, 
				"status"	=> $stat,
				"start_date"=> $this->jCfg['isearch']['start_date'],
				"end_date"	=> $this->jCfg['isearch']['end_date'],
				"offset" 	=> $this->uri->segment($this->uri_segment)
		) );
		
		$data = $this->_data(array(
				'base_url'		=> $this->own_link.'index'
			)
		);	
					
		$this->_v($this->folder_view.$this->prefix_view,$data);
	}
	
	function search(){
		$this->_set_desc_title('Daftar Penawaran Formulir Kepada Asuransi');	
		$this->load->helper('url');		
		$this->uri_segment = 6;
			
		$column = ''; $keyword = '';				
		if( isset($_POST['column']) && isset($_POST['keyword']) ){
			$column = $_POST['column'];
			$keyword = url_title($_POST['keyword']);
			$this->setiSearch(array(
				"start_date"=> dbClean($_POST['dstart']),
				"end_date"	=> dbClean($_POST['dend']),
				"status"	=> dbClean($_POST['status']),
				"per_page"	=> dbClean($_POST['per_page'])
			));
		}else{
			$column =  $this->uri->segment(4);
			$keyword =   $this->uri->segment(5);
		}	
		
		$stat = ($this->jCfg['isearch']['status']=="all")?$this->status_form:"'".$this->jCfg['isearch']['status']."'";
		
		$this->per_page = $this->jCfg['isearch']['per_page'];
		$this->data_table = $this->A->data( array(
								"limit" 	=> $this->per_page, 
								"offset" 	=> $this->uri->segment($this->uri_segment),
								"column"	=> $column,
								"start_date"=> $this->jCfg['isearch']['start_date'],
								"end_date"	=> $this->jCfg['isearch']['end_date'],
								"status"	=> $stat,
								"keyword"	=> (trim($keyword)=="all")?"":$keyword
							) );
		$k=($keyword=="")?"all":$keyword;
		$data = $this->_data(
			array(
				'base_url'		=>$this->own_link.'search/'.$column.'/'.$k
			)
		);	
		
		
		$this->header_page['key']=pTxt(($k=="all")?"":$k);
		$this->header_page['col']=$column;	
		$this->header_page['cat_search'] = array($column	=>	$this->cat_search);	
		$this->_v($this->folder_view.$this->prefix_view,$data);
	}
		
	function edit($id=''){
		$id=dbClean(trim($id));
		$this->_set_desc_title('Update Data Penawaran Formulir Kepada Asuransi');	
		
		if(trim($id)!=''){
			$this->data_form = $this->DATA->data_id(array(
					'form_id'	=> $id
				));
			
			$this->DATA->table = "tm_pialang_marketing";
			$marketing = $this->DATA->_getall();
			
			$this->DATA->table = "tm_currency";
			$curr = $this->DATA->_getall();
			
			$this->DATA->table = "tm_file";
			$file = $this->DATA->_getall(array(
				"file_form_id"	=> $id
			));
			
			$asuransi = $this->A->get_asuransi(array(
				'type'	=> 'bank_garansi',
				'group'	=> 'a.asuransi_id '
			));
			
			$this->DATA->table = "tm_asuransi_form";
			$form_asuransi = $this->DATA->_getall(array(
				"as_form_id"	=> $id
			));
							
			$this->_v($this->folder_view.$this->prefix_view."_form",array(
					"marketing"	=> $marketing,
					"curr"		=> $curr,
					"asuransi"	=> $asuransi,
					"o_asuransi"	=> $form_asuransi,
					"files"		=> $file
			));
		}else{
			redirect($this->own_link);
		}
	}	
	
	function detail($id=''){
		$this->is_detail = TRUE;
		$this->dis = 'disabled="disabled"';
		
		$id=dbClean(trim($id));
		
		if(trim($id)!=''){
			$this->data_form = $this->DATA->data_id(array(
					'form_id'	=> $id
				));
			
			$this->DATA->table = "tm_pialang_marketing";
			$marketing = $this->DATA->_getall();
			
			$this->DATA->table = "tm_currency";
			$curr = $this->DATA->_getall();
			
			$this->DATA->table = "tm_file";
			$file = $this->DATA->_getall(array(
				"file_form_id"	=> $id
			));
			
			$asuransi = $this->A->get_asuransi(array(
				'type'	=> 'bank_garansi',
				'group'	=> 'a.asuransi_id '
			));
			
			$this->DATA->table = "tm_asuransi_form";
			$form_asuransi = $this->DATA->_getall(array(
				"as_form_id"	=> $id
			));
							
			$this->_v($this->folder_view.$this->prefix_view."_form",array(
					"marketing"	=> $marketing,
					"curr"		=> $curr,
					"asuransi"	=> $asuransi,
					"o_asuransi"	=> $form_asuransi,
					"files"		=> $file
			));
		}else{
			redirect($this->own_link);
		}
	}
	
	function save(){	
		$data = array(
			'form_status' 				=> dbClean($_POST['form_status'])
		);
				
		if(count($_POST['form_asuransi']) > 0){
			$this->DATA->table = "tm_asuransi_form";
			$this->DATA->_delete(array(
				'as_form_id'	=> dbClean($_POST['form_id'])
			));
			
			foreach($_POST['form_asuransi'] as $r){
				$df = array(
					'as_form_id'			=>  dbClean($_POST['form_id']),
					'as_asuransi_id'		=>  $r,
					'as_status'				=>  dbClean($_POST['form_status']),
					'time_add'				=>  date("Y-m-d H:i:s"),
					'user_add'				=>  $this->jCfg['user']['id'],
					'app_id'				=>  $this->jCfg['app_id']
				);
				$this->DATA->_add($df);
				
				// send mail ...
				if(cfg('online')==1 && dbClean($_POST['form_status']) == 'UPLOAD'){
					$email_asuransi = get_email_asuransi($r);
					$msg = "Anda telah mendapatkan penawaran pada tanggal : ".date("Y-m-D H:i:s");
					$msg .= "\n Silahkan buka website untuk melihat penawaran tersebut";
					$this->sendEmail(array(
						"type"		=> "text",
						"to"		=> $email_asuransi,
						"from"		=> getCfgApp('email_noreply'),
						"bcc"		=> getCfgApp('email_bcc'),
						"subject"	=> "Penawaran Formulir ",
						"title"		=> "Penawaran Formulir ",
						"message"	=> $msg
					));
				}
				
			}
			
		}
		
		$this->DATA->table = "tm_formulir";
		$a = $this->_save_master( 
			$data,
			array(
				'form_id' => dbClean($_POST['form_id'])
			),
			dbClean($_POST['form_id'])			
		);
		
						
		redirect($this->own_link);
	}
	
}
